// index.js
var app = getApp()
var util = require('../../utils/util.js');
var calendar_js = require('../../utils/calendar.js');
const day = ["周日", "周一", "周二", '周三', '周四', '周五', '周六', "周日"]
const da = new Date
da.setTime(da.getTime() - da.getTime() % (24 * 60 * 60 * 1000) - 8 * 60 * 60 * 1000);
const now = da.getTime();

Page({
  data: {
    toView: 'red',
    scrollTop: 100,
    stamp: [],
    date: "",
    movaFlag: true,
    choosedate: "",
    scrollitem: "",
    day: day,
    lastStamp: [],
    lastDates: [],
    dates: [],
    nextDates: [],
    nextStamp: [],
    year: '',
    month: '',
    page: 2,
    pages: [1, 2, 3, 4], indicatorDots: false,
    autoplay: false,
    index: 1,
    now: now,
    share: ["转发到微信", "生成朋友圈分享图", "生成二维码"],
    duration: 500,
    selected_date: now,
    editable: false,
    subscribe_flag: false,
  },
  pagechange: function (e) {
    var that = this;
    var thispages = that.data.pages;
    var time = that.data.stamp[0];
    var index = e.detail.current;
    // console.log("index:" + that.data.index + " ,newindex" + index);
    // console.log(thispages);
    //向后
    if (index == thispages.length - 1) {
      console.log("向后");
      getDates(that, (time + 7 * 24 * 60 * 60 * 1000));
      var pages = [1, 2, 3, 4];
      pages = thispages.concat(pages);

      that.setData({
        pages: pages,
        page: pages[index],
        index: index
      })
      // console.log("page1" + pages[index]);
    }//向前
    else if (index == 0) {
      console.log("向前");
      getDates(that, (time - 7 * 24 * 60 * 60 * 1000));
      var pages = [1, 2, 3, 4];
      pages = pages.concat(thispages);

      that.setData({
        pages: pages,
        page: 1,
        index: 4
      })
      // console.log("page2" + pages[index]);
    } else if (index != that.data.index) {
      console.log("向");
      index - that.data.index > 0 ? getDates(that, time + 7 * 24 * 60 * 60 * 1000) : getDates(that, time - 7 * 24 * 60 * 60 * 1000);
      that.setData({
        page: thispages[index],
        index: index
      })
      // console.log("page3" + thispages[index]);
    }
    console.log(that.data.choosedate);
  },
  onLoad: function (option) {
    var that = this;
    var calendar_id = option.id;
    console.log(option);
    // wx.createSelectorQuery().select('.schedule-swiper').boundingClientRect(function (rect) {
    //   console.log(123);
    //   rect.id      // 节点的ID
    //   rect.dataset // 节点的dataset
    //   rect.left    // 节点的左边界坐标
    //   rect.right   // 节点的右边界坐标
    //   rect.top     // 节点的上边界坐标
    //   rect.bottom  // 节点的下边界坐标
    //   rect.width   // 节点的宽度
    //   rect.height  // 节点的高度
    //   console.log(rect);
    // }).exec()
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
    console.log("schedule  ", option);
    getDates(that, option.time);

    this.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth,
      windowHeight: wx.getSystemInfoSync().windowHeight,
      scolloritemWidth: (wx.getSystemInfoSync().windowWidth - 35) / 7,
      calendar_id: calendar_id,
    })

  }, 
  onUnload: function (e) {

  },

  bindthatdate: function (e) {
    var formatdate = e.detail.value;
    var stamp = util.formatTimeStamp(formatdate, "");

    getDates(this, stamp);

    // var that = this;
    // var d = new Date;
    // var flag = false;
    // var thisstamp = this.data.stamp;

    // console.log("bindthatdate");
    // d.setTime(stamp);
    // var day = d.getDay();
    // if (stamp >= thisstamp[0]) {
    //   if (stamp - thisstamp[0] < (24 * 60 * 60 * 1000 * 7)) {
    //     if (day >= 0) {
    //       console.log(1);
    //       that.setData({
    //         thatdate: formatdate,
    //         choosedate: day,
    //         date: that.data.dates[day],
    //       })
    //     } else {

    //       console.log(day);
    //     }
    //   }
    // }
    // else {
    //   if (thisstamp[0] - stamp < (24 * 60 * 60 * 1000 * 7)) {
    //     if (day <= 0) {
    //       console.log(2);
    //       that.setData({
    //         thatdate: formatdate,
    //         choosedate: day,
    //         date: that.data.dates[day],
    //       })
    //     }else{
    //      wx.redirectTo({
    //        url: 'index?time=' + stamp,

    //      })
    //      console.log(day);
    //     }
    //   }
    // }
  },
  backtoday: function (e) {
    getDates(this, now);
  },
  share: function (e) {
    getDates(this, now);
  },
  bindpage: function (e) {
    var that = this;
    console.log(e);
    var date = new Date;
    date.setTime(e.currentTarget.dataset.stamp);
    console.log(e.currentTarget.dataset.stamp);
    console.log(date);
    var thatdate = util.formatDate(date);
    date = date.getDate();
    this.setData({
      thatdate: thatdate,
      date: date,
      choosedate: e.currentTarget.dataset.id,
      selected_date: e.currentTarget.dataset.stamp,
    })
    console.log(thatdate);
    console.log(date);
    that.refresh_activity_show();
  },
  create_activity: function () {
    wx.navigateTo({
      url: '../create/index'
    })
  },
  onShow: function () {
    var that = this;
    var calendar_id = that.data.calendar_id;
    //console.log(that.data.selected_date);
    that.refresh_activity_show();
    that.check_subscribe();
    calendar_js.view(calendar_id, function (res){
      console.log(res);
      if (res.data.op == 'calendar_view') {
        var calendar = res.data.data.info;
        var editable = res.data.data.editable;
        that.setData({
          type: calendar.type,
          calendar_title: calendar.title,
          calendar_content: calendar.content,
          owner_id: calendar.owner.id,
          activity_list: calendar.activity_detail_list_by_date,
          owner_name: calendar.owner.name,
          owner_avatar: calendar.owner.avatar,
          editable: editable,
        });
      }
    });
  },
  refresh_activity_show: function () {
    var that = this;
    var selected_date = that.data.selected_date;
    var selected_date_stamp = get_date_stamp(selected_date);
    that.setData({
      selected_date: selected_date_stamp * 1000,
    });
    console.log(selected_date_stamp);
  },
  wanna_unsubcribe: function () {
    var that = this;
    var cid = that.data.calendar_id;
    calendar_js.unsubscribe(cid, function (res) {
      console.log(res);
      if (res.data.op == 'calendar_unsubscribe') {
        that.refresh_subscribe_flag(res.data.data);
      }
    });
  },
  wanna_subcribe: function () {
    var that = this;
    var cid = that.data.calendar_id;
    calendar_js.subscribe(cid, function (res) {
      console.log(res);
      if (res.data.op == 'calendar_subsrcibe') {
        that.refresh_subscribe_flag(res.data.data);
      }
    });
  },
  check_subscribe: function () {
    var that = this;
    var cid = that.data.calendar_id;
    calendar_js.check_subscribe(cid, function (res) {
      console.log(res);
      if (res.data.op == 'calendar_check_subscribe') {
        that.refresh_subscribe_flag(res.data.data);
      }
    });
  },
  refresh_subscribe_flag: function (data) {
    var that = this;
    console.log(data);
    if (data == null) {
      that.setData({
        subscribe_flag: false
      });
    } else {
      that.setData({
        subscribe_flag: true
      });
    }
  },

})

function getDates(that, time) {
  var dates = [];
  var date = new Date;


  var stamp = [];
  if (time != undefined) {
    // console.log(time);
    date.setTime(time);
    // console.log(date.getTime() );

  }
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  date.setTime((date.getTime() / (24 * 60 * 60 * 1000)) * (24 * 60 * 60 * 1000));

  var lastStamp = [];
  var nextStamp = [];

  console.log("getDay");

  var today = date.getDay();

  var time = date.getTime();
  var thatdate = util.formatDate(date);
  if (today != 0) {
    date.setTime(time - (today) * 24 * 60 * 60 * 1000);
  }
  date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);

  var lastDates = [];
  for (var i = 0; i < 7; i++) {
    lastStamp.push(date.getTime());
    lastDates.push(date.getDate());
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
  }
  for (var i = 0; i < 7; i++) {
    stamp.push(date.getTime())
    dates.push(date.getDate());
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
  }
  var nextDates = [];
  for (var i = 0; i < 7; i++) {
    nextStamp.push(date.getTime());
    nextDates.push(date.getDate());
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
  }
  that.setData({
    dates: dates,
    date: dates[today],
    stamp: stamp,
    thatdate: thatdate
    , choosedate: today,
    lastDates: lastDates,
    nextDates: nextDates,
    nextStamp: nextStamp,
    lastStamp: lastStamp,
    year: year,
    month: month,
  })
  // console.log(date.getTime())
  // console.log(lastDates);
  // console.log(dates);
  // console.log(nextDates);
}

function get_date_stamp(stamp) {
  console.log(stamp);
  stamp = (stamp - stamp % 1000) / 1000;
  stamp = stamp - (stamp + 8 * 60 *60 ) % (60 * 60 * 24); 
  console.log(stamp);
  return stamp;
}